<?php
namespace Pub\wx;
abstract class Wx
{
	protected $_appid = '';
	protected $_secret = '';


	public function __construct()
	{
		global $_W;
		$_account = pdo_get('kuyuan_vbz_setting',array('uniacid'=>$_W['account']['uniacid']),array('appid','app_secret'));
		$this->_appid = $_account['appid'];
		$this->_secret = $_account['appsecret'];
	}

	/**
	 * 获取accessToken
	 * @return mixed
	 */
	public function getAccessToken()
	{
		$account_api = WeAccount::create();
		return $account_api->getAccessToken();
	}


	/**
	 *
	 * @param unknown $url
	 * @param unknown $vars
	 * @param string $_cert  //是否带上证书
	 * @param number $second
	 * @param array $aHeader
	 * @return mixed|boolean
	 */
	protected function wxHttpsRequestPem($url, $vars = '',$_cert = false, $second=30,$aHeader=array()){
		$ch = curl_init();
		//超时时间
		curl_setopt($ch,CURLOPT_TIMEOUT,$second);
		curl_setopt($ch,CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($ch,CURLOPT_URL,$url);
		curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);
		curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,false);
		if($_cert){
			//以下两种方式需选择一种
			//第一种方法，cert 与 key 分别属于两个.pem文件
			//默认格式为PEM，可以注释
			curl_setopt($ch,CURLOPT_SSLCERTTYPE,'PEM');
			curl_setopt($ch,CURLOPT_SSLCERT,$this->_cert_cert);
			//默认格式为PEM，可以注释
			curl_setopt($ch,CURLOPT_SSLKEYTYPE,'PEM');
			curl_setopt($ch,CURLOPT_SSLKEY,$this->_cert_key);
			curl_setopt($ch,CURLOPT_CAINFO,'PEM');
			//($ch,CURLOPT_CAINFO,ATTACHMENT_ROOT.$this->_ca);
			//第二种方式，两个文件合成一个.pem文件
			//curl_setopt($ch,CURLOPT_SSLCERT,getcwd().'/all.pem');
		}
		if(count($aHeader) >= 1 ){
			curl_setopt($ch, CURLOPT_HTTPHEADER, $aHeader);
		}
		if(!empty($vars)){
			curl_setopt($ch,CURLOPT_POST, 1);
			curl_setopt($ch,CURLOPT_POSTFIELDS,$vars);
		}
		$data = curl_exec($ch);
		if($data){
			curl_close($ch);
			return $data;
		} else {
			$error = curl_errno($ch);
			echo "call faild, errorCode:$error\n";
			curl_close($ch);
			return false;
		}
	}
}